/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

isUsernameUsed = false;

function submitForm() {
    var valid = "ok";
    var validEmail = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
    
    if ($('username').value=="" || $('password').value=="" || $('confirm').value=="" || $('fullname').value=="" || $('email').value=="" || !$('agreed').checked) {
        valid = "Anda belum mengisi semua form.";
    } else if ($('password').value != $('confirm').value) {
        valid = "Harap ketikkan ulang password Anda.";
        $('password').value = "";
        $('confirm').value = "";
    } else if (!validEmail.test($('email').value)) {
        valid = "Pastikan kembali apakah email <br /> Anda benar.";
    } else if (isUsernameUsed) {
        valid = "Username yang Anda masukkan <br /> sudah digunakan sebelumnya. <br />Harap masukkan username lain.";
    } else
        valid = "ok";

    if (valid == "ok") 
        $('registerform').submit();
    
    else
        $('loadingatloginform').innerHTML = valid;
            
}

function validateUsernameUsed() {
    $('usernameused').innerHTML="Loading...";
    var validateUsername = new XMLHttpRequest();
    var validatingName = $('username').value;
    validateUsername.open("get", "handler/checkusername.jsp?name="+$('username').value, true);
    validateUsername.send(null);
    validateUsername.onreadystatechange = function() {
        if (validatingName == $('username').value) {
            if (validateUsername.readyState == 4) {
                var responseXML = validateUsername.responseXML;
                var isExist = responseXML.getElementsByTagName("root")[0].childNodes[0].nodeValue;
                if (isExist == "true") {
                    $('usernameused').innerHTML="<span style=\"color:green\">OK</span>";
                    isUsernameUsed = false;
                }
                else {
                    $('usernameused').innerHTML="<span style=\"color:red\">That username has already been used.</span>";
                    isUsernameUsed = true;
                }
            }
            if ($('username').value == "") {
                $('usernameused').innerHTML="<span style=\"color:red\">&nbsp;</span>";
            }
        }
    }
}
